#!/usr/bin/env /Library/ringojs/bin/ringo

/**
 * @fileoverview
 * Script to parallelize multiple builds of different permutations.
 */

addToClasspath('/devel/clbin/compiler.jar');

/* Ringo imports  */
var {command} = require('ringo/subprocess');

/* Java imports */
var {Level, Logger} = java.util.logging,
    {List} = java.util,
    {
      Compiler, JSSourceFile, CommandLineRunner, CompilationLevel, CheckLevel,
      WarningLevel, CompilerOptions, ClosureCodingConvention, Result,
      PrintStreamErrorManager
    } = com.google.javascript.jscomp;

/*
var closureDepsPath = '../../closure-library-read-only/closure/goog/deps.js',
    dkpointsDepsPath = 'dkpoints-deps.js',
    builderCmd = ['closurebuilder.py',
                 '--root=../../closure-library-read-only/closure/',
                 '--root=../../closure-library-read-only/third_party/closure/',
                 '--root=dkpoints/',
                 '-i ' + closureDepsPath,
                 '-i dkpoints/main.js'],
    fileList = command(builderCmd.join(' ')).split('\n');

Logger.getLogger('com.google.javascript.jscomp').setLevel(Level.OFF);

var compiler = new Compiler(),
    inputs = new java.util.ArrayList();
    
inputs.add(JSSourceFile.fromFile(closureDepsPath));
inputs.add(JSSourceFile.fromFile(dkpointsDepsPath));
for (var i = 0, len = fileList.length; i < len; ++i) {
  try {
    if(fileList[i] !== '') {
      inputs.add(JSSourceFile.fromFile(fileList[i]));
    }
  }
  catch (e) {
    print('Exception: ' + e.message);
    print('File name: ' + fileList[i]);
  }
}
*/
var externs = CommandLineRunner.getDefaultExterns(),
    options = new CompilerOptions();

CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
WarningLevel.VERBOSE.setOptionsForWarningLevel(options);
options.setCodingConvention(new ClosureCodingConvention());


print(options.toString(), '\n');

/*
 * Colorize Errors!
 *
var {System} = java.lang;
if(System.getProperty('os.name').indexOf('Windows') === -1) {
  let formatter = 
      new com.google.javascript.jscomp.LightweightMessageFormatter(compiler);

  formatter.setColorize(true);
  compiler.setErrorManager(new PrintStreamErrorManager(formatter, System.err));
}

/*
var results = compiler.compile(externs, inputs, options);
if(results.success) {
  require('fs').write('compiled.js', compiler.toSource(),
                      {write: true, charset: 'utf8'});

  print('Compile complete.');
}
else {
  print('Crap...');
}
*/